#include "Scene/Scene.h"

#include <glut.h>

namespace vital {

	//--------------------------------------------------------------------------
	TScene::TScene() {

	}

	//--------------------------------------------------------------------------
	TScene::~TScene() {

		clearObjects();
	}

	//--------------------------------------------------------------------------
	void TScene::addObject(const std::string &name, TSceneObject *obj, unsigned long visFlags) {

		obj->setVisFlags(visFlags);
		mObjects.insert( std::make_pair(name, obj) );
	}

	//--------------------------------------------------------------------------
	TSceneObject* TScene::getObjectByName(const std::string &name) {

		Map_t::iterator iter = mObjects.find(name);

		if(iter == mObjects.end() )
			return NULL;

		return iter->second;
	}

	//--------------------------------------------------------------------------
	void TScene::clearObjects() {

		Map_t::iterator iter = mObjects.begin();

		while(iter != mObjects.end() ) {

			delete iter->second;

			iter++;
		}

		mObjects.clear();
	}

	//--------------------------------------------------------------------------
	void TScene::step(float dt) {

	}

	//--------------------------------------------------------------------------
	void TScene::beginRender(TCamera *camera) {

		glMatrixMode(GL_PROJECTION);
		glLoadMatrixf( camera->getProjMatrix().getData() );

		glMatrixMode(GL_MODELVIEW);
		glLoadMatrixf( camera->getViewMatrix().getData() );
	}

	//--------------------------------------------------------------------------
	void TScene::render(unsigned long visMask) {

		Map_t::iterator iter = mObjects.begin();

		while(iter != mObjects.end() ) {

			TSceneObject *obj = iter->second;

			glPushMatrix();

			glMultMatrixf( obj->getWorldTransform().getData() );

			if( obj->isVisible(visMask) )
				obj->render();

			glPopMatrix();

			iter++;
		}
	}

	//--------------------------------------------------------------------------
	void TScene::endRender() {

	}

	//--------------------------------------------------------------------------

}